package com.admin.web.controller;

import com.admin.web.model.AjaxJson;
import com.admin.web.model.LoginUser;
import com.admin.web.model.RoleRequest;
import com.admin.web.uresponse.OBGUserResponse;
import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.rbac.OBGPermissionsInfo;
import com.bayss.core.entity.rbac.OBGRoleInfo;
import com.bayss.core.entity.rbac.OBGUserInfo;
import com.bayss.core.entity.rbac.query.AdminQuery;
import com.bayss.core.model.UserInfoVo;
import com.bayss.core.service.RbacService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理接口类
 */
@RestController
public class RbacController extends ApiBaseController {
    @Autowired
    private RbacService rbacService;
    @Value("${token.outHours}")
    private Integer outHours;


    /**
     * 管理员登录
     *
     * @param account
     * @param password
     * @return
     */
    @RequestMapping(value = "/rbac/login.json", method = RequestMethod.POST)
    public ResultMsg Login(@RequestParam(value = "account") String account,
                           @RequestParam(value = "password") String password) {
        try {
            OBGUserInfo userInfo = rbacService.loginAdmin(account, password, outHours);
            if (userInfo == null) {
                return ResultMsg.fail(250, "用户名或密码有误");
            }

            //查下角色权限
            List<OBGPermissionsInfo> permissionsInfoList = rbacService.treePermission(null, userInfo.getId());
            List<OBGRoleInfo> roleList = rbacService.findRolesByUser(userInfo.getId());

            LoginUser loginUser = new LoginUser(userInfo, permissionsInfoList, roleList);
            return ResultMsg.success(loginUser);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "登录失败");
        }
    }


    /**
     * 分页查询管理人员列表
     *
     * @param adminQuery
     * @return
     */
    @RequestMapping(value = "/rbac/listAdmin.json", method = RequestMethod.POST)
    public AjaxJson listAdmin(@RequestBody AdminQuery adminQuery) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            PageInfo<OBGUserInfo> obgUserInfos = rbacService.listAdmin(adminQuery);
            ajaxJson.setData(obgUserInfos);
            ajaxJson.setStatus(200).setMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("提交失败");
        }
        return ajaxJson;
    }

    /**
     * 新增管理员
     *
     * @param userInfoVo
     * @return
     */
    @RequestMapping(value = "/rbac/addAdmin.json", method = RequestMethod.POST)
    public ResultMsg addAdmin(@RequestBody UserInfoVo userInfoVo) {
        return rbacService.addAdmin(userInfoVo);
    }


    /**
     * 编辑管理员
     */
    @RequestMapping(value = "/rbac/editAdmin.json", method = RequestMethod.POST)
    public ResultMsg editAdmin(@RequestBody UserInfoVo userInfoVo) {
        return rbacService.editUser(userInfoVo);
    }

    /**
     * 删除管理员
     *
     * @param uid
     * @return
     */
    @RequestMapping(value = "/rbac/delAdmin.json", method = RequestMethod.POST)
    public AjaxJson delAdmin(@RequestParam(value = "uid", defaultValue = "", required = false) Integer uid) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            rbacService.delAdmin(uid);
            ajaxJson.setStatus(200).setMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("提交失败");
        }
        return ajaxJson;
    }

    /**
     * 批量删除管理员
     */
    @RequestMapping(value = "/rbac/batchDelAdmin.json", method = RequestMethod.POST)
    public ResultMsg batchDelAdmin(@RequestParam(value = "uids", defaultValue = "", required = false) String uids) {
        for (String id : uids.split(",")) {
            try {
                rbacService.delAdmin(Integer.valueOf(id));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ResultMsg.success();
    }

    /**
     * 冻结管理员
     *
     * @param uid
     * @return
     */
    @RequestMapping(value = "/rbac/freezeAdmin.json", method = RequestMethod.POST)
    public AjaxJson freezeAdmin(@RequestParam(value = "uid", defaultValue = "", required = false) Integer uid) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            rbacService.adminStatus(uid, 0);
            ajaxJson.setStatus(200).setMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("提交失败");
        }
        return ajaxJson;
    }


    /**
     * 解冻管理员
     *
     * @param uid
     * @return
     */
    @RequestMapping(value = "/rbac/thawAdmin.json", method = RequestMethod.POST)
    public AjaxJson thawAdmin(@RequestParam(value = "uid", defaultValue = "", required = false) Integer uid) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            rbacService.adminStatus(uid, 1);
            ajaxJson.setStatus(200).setMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("提交失败");
        }
        return ajaxJson;
    }

    /**
     * 重置密码
     */
    @RequestMapping(value = "/rbac/resetPassword.json", method = RequestMethod.POST)
    public ResultMsg resetPassword(OBGUserResponse userResponse,
                                   @RequestParam(value = "passwordOld") String passwordOld,
                                   @RequestParam(value = "passwordNew") String passwordNew,
                                   @RequestParam(value = "passwordNew2") String passwordNew2) {
        return rbacService.resetPassword(userResponse.getUid(), passwordOld, passwordNew, passwordNew2);
    }

    /**
     * 查询角色列表
     *
     * @return
     */
    @RequestMapping(value = "/rbac/listRole.json", method = RequestMethod.GET)
    public ResultMsg listRole() {
        try {
            List<OBGRoleInfo> obgRoleInfos = rbacService.listRole();
            return ResultMsg.success(obgRoleInfos);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 角色分页查询
     */
    @RequestMapping(value = "/rbac/rolesByPage.json", method = RequestMethod.GET)
    public ResultMsg rolesByPage(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                 @RequestParam(value = "size", defaultValue = "10", required = false) Integer size) {
        try {
            PageInfo<OBGRoleInfo> obgRoleInfos = rbacService.findRolesByPage(page, size);
            return ResultMsg.success(obgRoleInfos);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 新增角色
     *
     * @param userResponse
     * @return
     */
    @RequestMapping(value = "/rbac/addRole.json", method = RequestMethod.POST)
    public ResultMsg addRole(OBGUserResponse userResponse, @RequestBody RoleRequest request) {
        try {
            OBGRoleInfo obgRoleInfo = new OBGRoleInfo();
            obgRoleInfo.setName(request.getName());
            obgRoleInfo.setOperatorId(userResponse.getUid());
            obgRoleInfo.setType(Byte.valueOf("1"));
            obgRoleInfo.setStatus(1);
            rbacService.addRole(obgRoleInfo, request.getPermissionIds());
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "操作失败");
        }
    }

    /**
     * 修改角色
     *
     * @param userResponse
     * @return
     */
    @RequestMapping(value = "/rbac/updateRole.json", method = RequestMethod.POST)
    public ResultMsg updateRole(OBGUserResponse userResponse, @RequestBody OBGRoleInfo obgRoleInfo) {
        try {
            rbacService.updateRole(obgRoleInfo, obgRoleInfo.getPermissionIds());
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "操作失败");
        }
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/rbac/delRole.json", method = RequestMethod.POST)
    public AjaxJson delRole(@RequestParam(value = "roleId", defaultValue = "", required = false) Integer roleId) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            rbacService.delRole(roleId);
            ajaxJson.setStatus(200).setMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("提交失败");
        }
        return ajaxJson;
    }

    /**
     * 冻结角色
     *
     * @param userResponse
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/rbac/freezeRole.json", method = RequestMethod.POST)
    public AjaxJson freezeRole(OBGUserResponse userResponse, @RequestParam(value = "roleId", defaultValue = "", required = false) Integer roleId) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            rbacService.roleStatus(roleId, 0);
            ajaxJson.setStatus(200).setMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("提交失败");
        }
        return ajaxJson;
    }

    /**
     * 解冻角色
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/rbac/thawRole.json", method = RequestMethod.POST)
    public AjaxJson thawRole(@RequestParam(value = "roleId", defaultValue = "", required = false) Integer roleId) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            rbacService.roleStatus(roleId, 1);
            ajaxJson.setStatus(200).setMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("提交失败");
        }
        return ajaxJson;
    }

    /**
     * 查询权限列表
     *
     * @param roleId
     * @param userId
     * @return
     */
    @RequestMapping(value = "/rbac/listPermission.json", method = RequestMethod.GET)
    public AjaxJson listPermission(@RequestParam(value = "roleId", defaultValue = "", required = false) Integer roleId,
                                   @RequestParam(value = "userId", defaultValue = "", required = false) Integer userId) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            List<OBGPermissionsInfo> permissionsInfos = rbacService.listPermission(roleId, userId);
            ajaxJson.setData(permissionsInfos);
            ajaxJson.setStatus(200).setMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("提交失败");
        }
        return ajaxJson;
    }

    /**
     * 查询权限树
     *
     * @param roleId
     * @param userId
     * @return
     */
    @RequestMapping(value = "/rbac/permissionTree.json", method = RequestMethod.GET)
    public ResultMsg permissionTree(@RequestParam(value = "roleId", defaultValue = "", required = false) Integer roleId,
                                    @RequestParam(value = "userId", defaultValue = "", required = false) Integer userId) {
        try {
            return ResultMsg.success(rbacService.treePermission(roleId, userId));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 权限删除
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/rbac/permission/delete.json")
    public AjaxJson permissionById(Integer id) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            rbacService.deleteOBGPermissionsInfoAndChildren(id);
            ajaxJson.setStatus(200).setMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("提交失败");
        }
        return ajaxJson;
    }

    /**
     * 新建menu
     */
    @PostMapping(value = "/rbac/permission/add.json")
    public ResultMsg permissionAdd(@RequestBody OBGPermissionsInfo obgPermissionsInfo) {
        return rbacService.addOBGPermissionsInfo(obgPermissionsInfo);
    }

    /**
     * 修改menu
     */
    @PostMapping(value = "/rbac/permission/update.json")
    public ResultMsg permissionUpdate(@RequestBody OBGPermissionsInfo obgPermissionsInfo) {
        rbacService.updateOBGPermissionsInfo(obgPermissionsInfo);
        return ResultMsg.success();
    }
}
